package com.csht.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csht.model.CshtUser;
import com.csht.page.PagerModel;
import com.csht.page.SystemContext;
import com.csht.service.CshtGoodsReportService;
import com.csht.service.CshtMessageReportService;
import com.csht.vo.my.CshtGoodsReportVo;
import com.csht.vo.my.CshtMessageReportVo;

/**
 * 举报记录
 * @author chenhuiyan
 *
 */
@Controller
@RequestMapping(value = "/toMy")
public class ReportController {
	
	@Autowired
	private CshtGoodsReportService cshtGoodsReportService;
	
	@Autowired
	private CshtMessageReportService cshtMessageReportService;

	/**
	 * 举报记录页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/report")
	public String report(ModelMap model, HttpServletRequest request) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser");
		Map<String, Integer> res = new HashMap<String, Integer>();
		res.put("userId", cshtUser.getUserId()); //获取用户id
		res.put("offset", SystemContext.getOffset()); //获取偏移量
		res.put("messagePageSize", SystemContext.getMessagePageSize()); //每页记录数
		PagerModel<CshtGoodsReportVo> pagerModelGoodsReport = cshtGoodsReportService.getGoodsReportByUserId(res); //获取商品举报数据
		PagerModel<CshtMessageReportVo> PagerModelMessageReport = cshtMessageReportService.getMessageReportByUserId(res); //获取留言举报数据
		//由于有两个PagerModel封装类，记录总数不一样，取出最大的做为分页总数量
		long goodsReporttotalSize = pagerModelGoodsReport.getTotalSize();
		long messageReporttotalSize = PagerModelMessageReport.getTotalSize();
		if(goodsReporttotalSize > messageReporttotalSize) {
			pagerModelGoodsReport.setTotalSize(goodsReporttotalSize);
			PagerModelMessageReport.setTotalSize(goodsReporttotalSize);
		}else {
			pagerModelGoodsReport.setTotalSize(messageReporttotalSize);
			PagerModelMessageReport.setTotalSize(messageReporttotalSize);
		}
		model.addAttribute("pagerModelGoodsReport", pagerModelGoodsReport);
		model.addAttribute("PagerModelMessageReport", PagerModelMessageReport);
		return "/toMy/report";
	}
	
	/**
	 * 根据记录id查找数据
	 * 参数格式"0-0",第一个数字"0"表示是商品举报类型，"1"表示是留言举报类型；第二个数字表示id编号
	 * @param typeAndId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getReportById")
	public Map<String, Object> getReportById(@RequestParam("typeAndId") String typeAndId) throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("reportType", "not"); //返回类型标记
		String[] arr = typeAndId.split("-"); //分割参数
		if(Integer.parseInt(arr[0]) == 0) { //0为商品举报类型
			CshtGoodsReportVo cshtGoodsReportVo = null;
			cshtGoodsReportVo = cshtGoodsReportService.getGoodsReportByGoodsReportId(Integer.parseInt(arr[1])); //查找商品举报表
			res.put("reportType", "goodsReport"); //返回类型标记
			res.put("cshtGoodsReportVo", cshtGoodsReportVo);
			return res;
		}else {
			CshtMessageReportVo cshtMessageReportVo = null;
			cshtMessageReportVo = cshtMessageReportService.getMessageReportByMessageReportId(Integer.parseInt(arr[1])); //查找留言举报表
			res.put("reportType", "messageReport"); //返回类型标记
			res.put("cshtMessageReportVo", cshtMessageReportVo);
			return res;
		}
	}
}
